package com.taylor.seckill.rabbitmq;

import com.taylor.seckill.config.RabbitmqConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author taylor
 * @Description: 消息生产者
 * @date 2021/4/17 12:49
 */
@Service
@Slf4j
public class MQSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 秒杀订单
     * @param msg
     */
    public void sendOrderMessage(Object msg) {
        log.info("send message:"+msg);
        rabbitTemplate.convertAndSend(RabbitmqConfig.TOPIC_EXCHANGE,"topic.taylor.swift",msg);
    }


    public void send(Object msg){
        log.info("发送消息："+msg.toString());
        rabbitTemplate.convertAndSend("queue",msg);
    }

    /**
     * fanout 模式发送消息
     * @param msg
     */
    public void sendFanout(Object msg){
        log.info("fanout模式发送消息："+ msg);
        rabbitTemplate.convertAndSend(RabbitmqConfig.FANOUT_EXCHANGE,"",msg);
    }

    public void sendDirect1(Object msg){
        log.info("direct模式发送消息："+msg);
        rabbitTemplate.convertAndSend(RabbitmqConfig.DIRECT_EXCHANGE,RabbitmqConfig.DIRECT_ROUTE_1,msg);
    }
    public void sendDirect2(Object msg){
        log.info("direct模式发送消息："+msg);
        rabbitTemplate.convertAndSend(RabbitmqConfig.DIRECT_EXCHANGE,RabbitmqConfig.DIRECT_ROUTE_2,msg);
    }

    public void sendTopic1(Object msg){
        log.info("topic 模式(匹配没有或者多个单词)发送消息："+msg);
        rabbitTemplate.convertAndSend(RabbitmqConfig.TOPIC_EXCHANGE,"topic.taylor.swift",msg);
    }
    public void sendTopic2(Object msg){
        log.info("topic 模式(匹配一个单词)发送消息："+msg);
        rabbitTemplate.convertAndSend(RabbitmqConfig.TOPIC_EXCHANGE,"swift.topic.taylor",msg);
    }
}
